#!/usr/bin/python
#coding:utf-8

#author: Freev

import linecache
import threading
from ftplib import FTP
import time


ftpfile=open('ftp1.txt','a')
hostpath='host.txt'   


def is_anonymous(ip,num):
    
    try:
        ftp =FTP(ip,timeout=3)
        if ftp.login() and len(ftp.nlst())>2:
            ftp.quit()
            num+=1
            print ("%s : %s ok"%(num,ip))
            ftpfile.write(ip+'\n')
            
        else : 
            print ("%s : %s nothing"%(num,ip))
            num+=1
            
    except Exception,ex:
        print ("%s : %s %s"%(num,ip,ex))
        num+=1


def getlin(li):
    con=linecache.getline(hostpath,li).strip('\n\r\t')
    if con: return con
    else: return None

def main():
    x=0
    a=len(linecache.getlines(hostpath))
    threads=[]
    th=20
    while x<a:
        flag=0
        for i in range(x,th+x):
            if getlin(i+1):
                flag+=1
                temp=threading.Thread(target=is_anonymous,args=(getlin(i+1),i))
                threads.append(temp)
            else: break
        for i in range(x,len(threads)):
            threads[i].start()
        for i in range(x,len(threads)):
            threads[i].join()
        x=x+th
        
if __name__=='__main__':
    main()
